package com.dhcc.sdc.cdss.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.dao.CdcOrdmastMapper;
import com.dhcc.cdc.busi.entity.CdcOrdmastEntity;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.sdc.cdss.dao.SdcCdssExamMapper;
import com.dhcc.sdc.cdss.dao.SdcCdssExamMatchMapper;
import com.dhcc.sdc.cdss.entity.SdcCdssExamEntity;
import com.dhcc.sdc.cdss.entity.SdcCdssExamMatchEntity;
import com.dhcc.sdc.cdss.service.ISdcCdssExamMatchService;
import com.dhcc.sds.standard.dto.CdcOrdmastDto;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 表数据服务层接口
 *
 * @ClassName: SdcCdssExamMatchServiceImpl
 * @Description: TODO
 * @author zhangdc
 * @date 2022-06-23 14:46:12
 */
@Service("sdcCdssExamMatchService")
public class SdcCdssExamMatchServiceImpl extends BaseServiceImpl<SdcCdssExamMatchMapper, SdcCdssExamMatchEntity> implements ISdcCdssExamMatchService {

    @Resource
    private SdcCdssExamMapper cdssExamMapper;
    @Resource
    private CdcOrdmastMapper cdcOrdmastMapper;

    @Override
    public List<SdcCdssExamMatchEntity> page(Page<SdcCdssExamMatchEntity> page, Map<String, Object> map) {
        return this.baseMapper.page(page, map);
    }

    @Override
    public List<SdcCdssExamMatchEntity> list(Map<String, Object> map) {
        return this.baseMapper.list(map);
    }

    @Override
    public List<Long> compareByRegexp(String filterTxt, String keywordAndTxt, String keywordOrTxt, Long busOrderCateId) {
        return this.baseMapper.compareByRegexp(filterTxt, keywordAndTxt, keywordOrTxt, busOrderCateId);
    }

    @Override
    public List<CdcOrdmastDto> pageMatchOrder(Page<SdcCdssExamMatchEntity> page, Map<String, Object> params) {
        List<CdcOrdmastDto> cdcOrdmastDtos = CollUtil.newArrayList();

        String id = (String) params.get("id");
        String orderKey = "";
        if (!StringUtils.isEmpty(params.get("orderKey"))){
            orderKey = (String) params.get("orderKey");
        }
        SdcCdssExamEntity cdssExamEntity = cdssExamMapper.selectById(Long.parseLong(id));
        if (!StringUtils.isEmpty(cdssExamEntity)){
            params.put("itemId", cdssExamEntity.getItemId());
        }else {
            return null;
        }

        //默认查询已对照的
        List<SdcCdssExamMatchEntity> page1 = this.baseMapper.page(page, params);
        if (null != page1 && page1.size() > 0){
            for (SdcCdssExamMatchEntity cdssExamMatchEntity : page1){
                CdcOrdmastDto cdcOrdmastDto = new CdcOrdmastDto();
                CdcOrdmastEntity cdcOrdmastEntity = cdcOrdmastMapper.selectById(cdssExamMatchEntity.getOrderId());
                if (!StringUtils.isEmpty(orderKey) && !cdcOrdmastEntity.getDescription().contains(orderKey)){
                    continue;
                }
                BeanUtil.copyProperties(cdcOrdmastEntity,cdcOrdmastDto);
                cdcOrdmastDto.setChecked(1);
                cdcOrdmastDto.setIsDefault(cdssExamMatchEntity.getIsDefault());
                cdcOrdmastDtos.add(cdcOrdmastDto);
            }
        }

        return cdcOrdmastDtos;
    }

    @Override
    public List<CdcOrdmastDto> pageOrder(Page<CdcOrdmastEntity> page, Map<String, Object> params) {
        List<CdcOrdmastDto> res = CollUtil.newArrayList();
        String compare = (String) params.get("compare");
        String id = (String) params.get("id");
        String orderKey = "";
        if (!StringUtils.isEmpty(params.get("orderKey"))){
            orderKey = (String) params.get("orderKey");
        }
        params.put("description", orderKey);
        SdcCdssExamEntity cdssExamEntity = cdssExamMapper.selectById(Long.parseLong(id));
        if (!StringUtils.isEmpty(cdssExamEntity)){
            params.put("itemId", cdssExamEntity.getItemId());
        }else {
            return null;
        }

        if (Objects.equals("all", compare)){
            //查询所有
            List<SdcCdssExamMatchEntity> list = this.baseMapper.list(params);
            List<CdcOrdmastEntity> page1 = cdcOrdmastMapper.page(page, params);
            for (CdcOrdmastEntity cdcOrdmastEntity : page1){
                CdcOrdmastDto cdcOrdmastDto = new CdcOrdmastDto();
                BeanUtil.copyProperties(cdcOrdmastEntity,cdcOrdmastDto);
                res.add(cdcOrdmastDto);
            }

            if (list.size() == 0){

                return res;
            }else {
                for (CdcOrdmastDto cdcOrdmastDto : res){
                    for (SdcCdssExamMatchEntity cdssExamMatchEntity : list) {
                        if (Objects.equals(cdcOrdmastDto.getId(), cdssExamMatchEntity.getOrderId())) {
                            cdcOrdmastDto.setChecked(1);
                            cdcOrdmastDto.setIsDefault(cdssExamMatchEntity.getIsDefault());
                        }
                    }
                }
                return res;
            }
        }else if (Objects.equals("noMatch", compare)){
            //查询未对照的
            List<Long> matchOrdmastIds = this.baseMapper.selectMatchOrdmastIds(params);
            List<CdcOrdmastEntity> cdcLabOrderEntities = cdcOrdmastMapper.pageExcludeMatch(page, params, matchOrdmastIds);
            for (CdcOrdmastEntity cdcOrdmastEntity : cdcLabOrderEntities){
                CdcOrdmastDto cdcOrdmastDto = new CdcOrdmastDto();
                BeanUtil.copyProperties(cdcOrdmastEntity,cdcOrdmastDto);
                res.add(cdcOrdmastDto);
            }
        }

        return res;
    }
}
